package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.CustomerTransfer;
import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.mapper.CustomerTransferMapper;
import cn.wolfcode.ssm.query.QueryObject;
import cn.wolfcode.ssm.service.ICustomerTransferService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService {

    @Autowired
    private CustomerTransferMapper customerTransferMapper;


    @Autowired
    private CustomerServiceImpl customerService;

    @Override
    public void save(CustomerTransfer customerTransfer) {
        // 先把销售人员的id修改
        customerService.updateSeller(customerTransfer.getNewSeller().getId(),
                customerTransfer.getCustomer().getId());
        // 获取当前登陆对象
        Employee employee = (Employee)SecurityUtils.getSubject().getPrincipal();

        // 设置操作人员
        customerTransfer.setOperator(employee);

        // 设置移交时间
        customerTransfer.setOperateTime(new Date());

        // 在插入对象
        customerTransferMapper.insert(customerTransfer);
    }

    @Override
    public void delete(Long id) {
        customerTransferMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(CustomerTransfer customerTransfer) {
        customerTransferMapper.updateByPrimaryKey(customerTransfer);
    }

    @Override
    public CustomerTransfer get(Long id) {
        return customerTransferMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTransfer> listAll() {
        return customerTransferMapper.selectAll();
    }

    @Override
    public PageInfo<CustomerTransfer> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<CustomerTransfer> customerTransfers = customerTransferMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<CustomerTransfer>(customerTransfers);
    }
}
